<template>
  <div>
    <el-form ref="deptForm" :rules="rulesDeptForm" :model="deptForm" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="deptForm.manager" placeholder="请选择部门负责人">
          <el-option
            v-for="item in employee"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ isEdit?'确认修改':'立即创建' }}</el-button>
        <el-button @click="$emit('cancleAddOrUpdata')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      default: () => { return [] }
    }
  },
  data() {
    // 1.校验部门编码在所有的模块中,不允许有重复
    const validCode = (rule, value, callback) => {
      // 首先把所有的编码组装成一个数组
      let exitCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        // 编辑的时候,把将编辑的那一项的code排除在外
        // 使用map方法 把code编码再次组装成一个新的数组
        exitCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      // 使用includes方法验证我们输入的编码是否在所有的编码中
      exitCodeList.includes(value) ? callback(new Error('部门编码' + value + '已存在')) : callback()
    }

    // 2.校验部门名称,同级下不能出现相同的部门名称 parentId就是点击节点本身的Id 在添加部门的时候,parentId就是本身的Id
    const validName = (rules, value, callback) => {
      let exitNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      if (this.isEdit) {
        const dept = this.originList.find(item => item.id === this.parentId)
        const pid = dept.pid
        exitNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }
      exitNameList.includes(value) ? callback(new Error('部门名称' + value + '已存在')) : callback

      // 利用filter筛选出同级部门(this.pid===this.parentId),再将同级部门的部门名称组装成一个数组
      // let exitNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      // if (this.isEdit) {
      //   // 找到当前元素
      //   const dept = this.originList.find(item => item.id === this.parentId)
      //   const pid = dept.pid
      //   // 找到所有的兄弟元素,并且排除自身
      //   exitNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      // }
      // // 使用includes方法验证我们输入的部门名称是否在同级部门名称中
      // exitNameList.includes(value) ? callback(new Error('部门名称' + value + '已存在')) : callback()
    }
    return {
      deptForm: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        manager: '', // 部门负责人
        name: '' // 部门名称
      },
      rulesDeptForm: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employee: [] // 部门负责人列表
    }
  },
  created() {
    this.getEmployeeSimple()
    this.getDepartDetail()
  },
  methods: {
    // 获取部门信息方法
    async getDepartDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        // console.log(res)
        this.deptForm = res.data
      }
    },
    // 新增/编辑部门信息
    onSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        if (this.isEdit) {
          const params = { ...this.deptForm }
          await updateDepartments(params)
        // console.log(res)
          // 编辑
        } else {
          //  新增
          const params = { ...this.deptForm, pid: this.parentId }
          await addDepartments(params)
        // console.log(res)
        }
        // 成功提示
        this.$message.success(this.isEdit ? '编辑成功' : '新增成功')
        // 关闭弹窗,重新加载页面,通过$emit事件
        this.$emit('close')
      })
    },

    // 获取部门负责人函数
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employee = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form{
  padding: 0 60px 0 30px;
}
.el-select {
    width: 100%;
}
</style>
